package com.beyond.pm.ui.home.config

import android.util.Log
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.MenuOpen
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.beyond.pm.bean.RouteScreenParameter
import com.beyond.pm.component.BaseSettingsItem
import com.beyond.pm.component.MyIconButton
import com.beyond.pm.component.MyTopBar
import com.beyond.pm.component.MyTopBarStyle
import com.beyond.pm.local.LocalNavController
import com.beyond.pm.local.AppNavHostController

/**
 * @author chen
 * @time 2023/5/31 13:34
 * @name ProjectManage
 * @class describe
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ConfigScreen(enter: Boolean, openDrawer: () -> Unit) {
    val nav = LocalNavController.current
    val appNav = remember(nav) { AppNavHostController(nav) }
    Scaffold(
        topBar = {
            MyTopBar(
                style = MyTopBarStyle.CenterAligned,
                navigationIcon = {
                    MyIconButton(
                        imageVector = Icons.Default.MenuOpen,
                        onClick = openDrawer
                    )
                },
                title = {
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.Center,
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Icon(
                            imageVector = RouteScreenParameter.Config.bean.icon,
                            contentDescription = null,
                        )
                        Spacer(modifier = Modifier.width(10.dp))
                        Text(text = stringResource(id = RouteScreenParameter.Config.bean.labelId))
                    }
                },
            )
        },
    ) {
        if (enter)
            ConfigContent(it, appNav)
    }
}

private val settings = listOf(
    RouteScreenParameter.User,
    RouteScreenParameter.Role,
    RouteScreenParameter.Menu,
    RouteScreenParameter.Banner,
    RouteScreenParameter.Parameter,
    RouteScreenParameter.Aggregation
)
@Composable
private fun ConfigContent(paddingValues: PaddingValues, appNav: AppNavHostController) {
    LazyColumn(
        modifier = Modifier
            .padding(
                PaddingValues(
                    start = 0.dp,
                    top = paddingValues.calculateTopPadding(),
                    end = 0.dp,
                    bottom = 0.dp
                )
            )
            .fillMaxSize(),
        verticalArrangement = Arrangement.spacedBy(4.dp),
    ) {
        item {
            settings.forEach { item ->
                BaseSettingsItem(
                    icon = rememberVectorPainter(image = item.bean.icon),
                    text = stringResource(id = item.bean.labelId),
                    onClick = {
                        Log.e("===>", item.bean.routerStr)
                        appNav.navigate(item.bean.routerStr)
                    }
                )
            }
        }
    }
}
